<template>
<el-dialog width="40%" title="添加/编辑服务" :visible.sync="serviceDialog" :show-close="false" :destroy-on-close="true" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="service" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        
        <el-form-item label="服务名称" prop="serviceTypeName">
          <el-input v-model="service.serviceTypeName"></el-input>
        </el-form-item>
        <el-form-item label="服务描述" prop="userName">
          <el-input v-model="service.serviceTypeDesc" ></el-input>
        </el-form-item>
         <el-form-item label="服务图标" prop="userName" class="flex flex-start upload-box">
           <qiniu-upload :imageUrl="service.serviceTypeIcon" @changeImage="changeIcon"></qiniu-upload>
        </el-form-item>
        <el-form-item label="服务状态" prop="type" >
          <el-select v-model="service.status" placeholder="服务状态" >
            <el-option label="正常" :value="1" key="1"></el-option>
            <el-option label="无效" :value="2" key="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="closeHandler()">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      </div>
</el-dialog>
   
</template>

<script>
import {serviceSaveUrl} from '@/utils/common'
import QiniuUpload from '../qiniu/upload.vue'
export default {
  props: {
    serviceDialog: {
      type: Boolean,
      default: false
    },
    popService: {
      type: Object,
      default: null
    }
  },
  watch: {
    popService() {
      this.service = this.popService; 

    }
  },
  components:{QiniuUpload},
  data() {
    
    return {
      loading: false,
      service: {
        serviceTypeId: "",
        serviceTypeName: "",
        serviceTypeDesc: "",
        serviceTypeIcon : "",
        status: 1,
      },
      rules: {
        serviceTypeName: { required: true, message: "请输入服务名称", trigger: "blur" },
      }
    }
  },
  mounted() {


  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveService();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    saveService() {
      this.loading = true;
      this.axios
        .post(serviceSaveUrl, this.service)
        .then(res => {
          if (res.data.status == "SUCCESS") {
            this.loading = false;
            this.$emit("close");
            this.service={}
            this.$message.success(res.data.result);
          } else {
            this.loading = false;
            this.$message.error(res.data.result);
          }
        })
        .catch(response => {
          this.loading = false;
          console.log("response - " + response);
          this.$message.error("数据保存异常失败");
        });
    },
    changeIcon(imageUrl){
      this.service.serviceTypeIcon=imageUrl;
    },
    closeHandler: function () {
      this.dialogVisible = false;
      this.user = {};
      this.$emit("close");
    }
  }
};
</script>

<style lang="less" scoped>
.upload-box{
    
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  /deep/.el-form-item__content{
    margin-left: 0!important;
  }
  
  }
</style>
